/*
 * HubInfoPinger.java
 *
 * Created on 11 September 2006, 21:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.sghdc;

import hubsniffer.ConnectHubThread;
import hubsniffer.DCHub;
import hubsniffer.Settings;
import libjdc.dc.client.User;
import hubsniffer.conn.HubInfoConnection;
import hubsniffer.db.DataBaseManager;
import hubsniffer.db.HubINFOWriter;
import hubsniffer.db.HubListReader;
import hubsniffer.db.PublicHubListGen;
import hubsniffer.util.Host;
import libjdc.dc.log.Log;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author root
 */
public class HubInfoPinger {
    
    private long delay;
    private DataBaseManager db;
    private Date start;
    private LinkedList<DCHub> hublist;
    
    public HubInfoPinger(Settings settings){
        /*Sniffer.setOutPutStream(System.out);
    Sniffer.setErrOutPutStream(System.err);*/
        hublist = new LinkedList<DCHub>();
        
        db = null;
        try {
            db = new DataBaseManager(settings.getDB_ADDRESS(), settings.getDB_USERNAME(), settings.getDB_PASSWD());
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        try {
            
            db.getHubClients();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        try {
            
            //Carrega a Lista de Hubs
            //hublist = new HubListReader(db).getHubListNacional();
            hublist = new HubListReader(db).getAllActiveHubs();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        start = new Date();
        delay = 5000;
        /*new HubInfoPinger(5000,start,db,hublist).*/startHubListPinguer();
        
        PublicHubListGen.updateHublist();
        System.exit(0);
    }
    
    public void startHubListPinguer(){
        Iterator<DCHub> hubIt = hublist.iterator();
        //start = new Date();
        
        // Faz a liga��o aos Hubs..
        while (hubIt.hasNext()) {
            DCHub hub = hubIt.next();
            User self;
            try {
                self = db.getSnifferUserFromHub(hub.getHub_id());
            } catch (SQLException ex) {
                ex.printStackTrace(Log.err);
                self = new User("[PT]sniffer","{HubSniffer Project: http://sghdc.no-ip.org/listhubs.php} <++ V:0.674,M:A,H:1/0/0,S:3>", "DSL","admin.sghdc@gmail.com", 104219547999L);                self.setHost(Host.getHostIP());
            }
            self.setHost(Host.getHostIP());
            self.getUserInfo().setDescription("http://sghdc.no-ip.org$http://sghdc.no-ip.org/download?file=PublicHubList$.xml$ $http://sghdc.no-ip.org/viewhub?hub_id="+hub.getHub_id()+"$HubSniffer Project: http://sghdc.no-ip.org/");
            new ConnectHubThread(new HubInfoConnection(hub,self),delay).start();
        }
        System.out.println("Vai esperar 10 segundos");
        try {
            Thread.currentThread().sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Terminaram os 10 segundos");
        
        hubIt = hublist.iterator();
        
        
        while (hubIt.hasNext())
            
            try {
                new HubINFOWriter(db).updateHubInfo(hubIt.next());
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        System.out.println("Terminou..");
    }
    
    public static void main(String[] args) throws ClassNotFoundException, FileNotFoundException {
        Log.setOutPutStream(System.out);
    Log.setErrOutPutStream(System.err);
    new HubInfoPinger(Settings.getInstance());
        
    }
    
}
